
#include "root_page.h"

#include "home_page.h"
#include "scene_page.h"
#include "screen_protection.h"


static ui_root_page_t urp = {

    .root_page   = NULL,
    .touch_timer = NULL,
};

void root_pgae_update_collection(void *data, lv_obj_t *item_view, int32_t index, uint8_t ui_type)
{
    debug_d("update\n");

    

}

void root_page_install_collection(void *data, lv_obj_t* item_view, int32_t index, uint8_t ui_type)
{
    debug_d("install-index=%d\n",index);
    page_info_t* page = NULL;
    if(!data){
        return -1;
    }

    page = (page_info_t*)data;
    page->method->on_page_create(item_view,page->data);

    return ;
}

void root_page_free_data(void *data)
{
    debug_d("free\n");

    page_info_t* page = NULL;
    if(!data){
        return -1;
    }

    page = (page_info_t*)data;
    page->method->on_page_destroy(page->data);
}

static void root_page_scroll_timer_callback(struct _lv_timer_t * t)
{
    if(urp.touch_timer){
        lv_timer_del(urp.touch_timer);
        urp.touch_timer = NULL;
    }
    //*********************************************

    urp.scroll_flag = 0;

    root_page_enable_scroll();
}

int root_page_switch_next(void)
{
    float          ratio = 0.0;
    lv_coord_t     dist  = 0;
    lv_coord_t     index = 0;
    lv_coord_t     pw    = lv_disp_get_hor_res(NULL);
    lv_coord_t     pt    = midea_collection_get_data_count(THE_DEVICE);

    // debug_d("pt    =%d\n",pt);
    debug_d("sindex=%d\n",urp.index);
    //*********************************************
    urp.x_e = lv_obj_get_scroll_x(urp.collection_content);
    debug_d("urp.x_e= %d\n",urp.x_e);
    //*********************************************
    dist    = urp.x_e%pw;
    ratio   = (float)abs(dist)/pw;

    if(dist <= 0){
        return -1;
    }

    debug_d("dist= %d\n",dist);
    //*********************************************
    if(urp.x_e > urp.x_s){
        debug_d("scroll left=%f\n",ratio);
        if(ratio > 0.25){
            urp.index++;
        }
        if(urp.index >= pt){
            urp.index = pt - 1;
        }
    }else{
        debug_d("scroll right=%f\n",ratio);
        if(ratio > 0.25){
            urp.index--;
            if(urp.index < 0){
                urp.index = 0;
            }
        }
    }
    //*********************************************
    debug_d("eindex=%d\n",urp.index);
    set_title_page_id(urp.index);
    //*********************************************
    scroll_collection_view_to_index_anim(THE_DEVICE,urp.index,0);
    //*********************************************
    urp.x_s = 0;
    urp.x_e = 0;
    //*********************************************


    return 0;
}

static void root_page_collection_view_callback(lv_event_t *e)
{
    void*  param = NULL;
    lv_indev_t* indev    = lv_indev_get_act();
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t *obj        = lv_event_get_target(e);
    lv_coord_t        pw = lv_disp_get_hor_res(NULL);

    // if(!indev){
    //     return ;
    // }

    param = lv_event_get_param(e);


    static int begin_flag = 0;

    switch(code){
    case LV_EVENT_SCROLL_BEGIN:
    {
        // debug_d("beg %x\n",param);
        if(!param){
            urp.x_s = lv_obj_get_scroll_x(urp.collection_content);
            debug_d("urp.x_s= %d\n",urp.x_s);
        }

        break;
    }
    case LV_EVENT_SCROLL:
    {
        // debug_d("scr\n");
        break;
    }
    case LV_EVENT_SCROLL_END:
    {
        debug_d("end %x\n",param);
        // 
        if(param){
            root_page_switch_next();
        }

        break;
    }
    case LV_EVENT_RELEASED:
    {
        debug_d("re\n");
        break;
    }
    default:break;
    }


    // switch(code){

    // case LV_EVENT_SCROLL_BEGIN:
    // {

    //     if(urp.touch_timer){
    //         lv_timer_reset(urp.touch_timer);
    //     }

    //     break;
    // }
    // case LV_EVENT_SCROLL:
    // {
    //     if(urp.touch_timer){
    //         lv_timer_reset(urp.touch_timer);
    //     }
    //     break; 
    // }
    // case LV_EVENT_SCROLL_END:
    // {
    //     debug_d("end\n");
    //     root_page_disable_scroll();
    //     root_page_switch_next();
    //     if(!urp.touch_timer){
    //         urp.touch_timer = lv_timer_create(root_page_scroll_timer_callback,60,NULL);
    //     }else{
    //         lv_timer_reset(urp.touch_timer);
    //     }

    //     if(urp.scroll_flag){
    //         break;
    //     }
    //     urp.scroll_flag = 1;


    //     // root_page_switch_next();

    //     break;
    // }
    // default:break;
    // }
}

static void root_page_show_callback(lv_anim_t *a)
{
    
}
int root_page_show(void)
{
    lv_coord_t ph = lv_disp_get_ver_res(NULL);

    if(!urp.root_page)
        return 0;

    lv_obj_clear_flag(urp.root_page,LV_OBJ_FLAG_HIDDEN);

    lv_obj_set_y(urp.root_page,ph); 

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, urp.root_page);
    lv_anim_set_values(&a,ph,0); 
    lv_anim_set_time(&a, 500); 
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y); 
    lv_anim_set_path_cb(&a, lv_anim_path_linear);
    lv_anim_set_ready_cb(&a, (lv_anim_exec_xcb_t)root_page_show_callback);
    lv_anim_start(&a);

    return 0;
}

static void root_page_hide_callback(lv_anim_t *a)
{
    lv_obj_add_flag(urp.root_page,LV_OBJ_FLAG_HIDDEN);        
}

int root_page_hide(void)
{
    lv_coord_t ph = lv_disp_get_ver_res(NULL);

    if(!urp.root_page)
        return 0;

    lv_obj_set_y(urp.root_page,0); 

    lv_anim_t a;
    lv_anim_init(&a);
    lv_anim_set_var(&a, urp.root_page);
    lv_anim_set_values(&a,0,ph); 
    lv_anim_set_time(&a, 300); 
    lv_anim_set_exec_cb(&a, (lv_anim_exec_xcb_t)lv_obj_set_y);
    lv_anim_set_path_cb(&a, lv_anim_path_ease_out);
    lv_anim_set_ready_cb(&a, (lv_anim_exec_xcb_t)root_page_hide_callback);
    lv_anim_start(&a);

    return 0;
}


static void root_page_gesture_cb(lv_event_t* e)
{
    lv_point_t vect;
    lv_obj_t *obj = NULL;
    lv_indev_t* indev = NULL;
    lv_event_code_t code = 0;
    lv_dir_t dir = LV_DIR_NONE;

    code  = lv_event_get_code(e);
    if(code != LV_EVENT_GESTURE)
        return ;

    debug_d("root gesture\n");

    dir = lv_indev_get_gesture_dir(lv_indev_get_act());
    switch(dir){
    case LV_DIR_BOTTOM:
    {
        root_gesture_bottom();
        break;
    }
    default:break;}
}

int root_page_disable_scroll(void)
{
    if(!urp.collection_content)
        return 0;

    lv_obj_clear_flag(urp.collection_content, LV_OBJ_FLAG_SCROLLABLE);
    return 0;
}

int root_page_enable_scroll(void)
{
    if(!urp.collection_content)
        return 0;

    lv_obj_add_flag(urp.collection_content, LV_OBJ_FLAG_SCROLLABLE);
    return 0;
}

int root_page_is_create(void)
{
    if(urp.root_page)
        return 1;
    else
        return 0;
}
int root_page_create(void)
{
    lv_coord_t pw = 0;
    lv_coord_t ph = 0;

    pw = lv_disp_get_hor_res(NULL);
    ph = lv_disp_get_ver_res(NULL);

    if(urp.root_page)
        return 0;

    urp.root_page = lv_obj_create(lv_scr_act());

    lv_obj_set_pos(urp.root_page,0,0);
    lv_obj_set_width(urp.root_page, pw);
    lv_obj_set_height(urp.root_page, ph);

    lv_obj_set_style_radius(urp.root_page, 0, 0);
    lv_obj_set_style_border_width(urp.root_page, 0, 0);
    lv_obj_set_style_bg_color(urp.root_page, lv_color_hex(0xe8380d), 0);
    lv_obj_set_style_bg_opa(urp.root_page, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_pad_all(urp.root_page, 0, 0);
    
    lv_obj_clear_flag(urp.root_page,LV_OBJ_FLAG_SCROLLABLE);

    #if 1
	lv_obj_set_style_bg_opa(urp.root_page, LV_OPA_0, LV_PART_MAIN);
    #else
	lv_obj_set_style_bg_opa(urp.root_page, LV_OPA_100, LV_PART_MAIN);
	lv_obj_set_style_bg_color(urp.root_page, lv_color_make(0xff,0,0xff), LV_PART_MAIN);
    #endif

    lv_obj_clear_flag(urp.root_page,LV_OBJ_FLAG_GESTURE_BUBBLE);
    lv_obj_add_event_cb(urp.root_page,root_page_gesture_cb,LV_EVENT_GESTURE,NULL);
    //************************************************
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_pad_bottom(&style, 0);
    lv_style_set_pad_top(&style, 0);
    lv_style_set_pad_left(&style, 0);
    lv_style_set_pad_right(&style, 0);
    lv_style_set_border_width(&style, 0);
    lv_style_set_bg_color(&style,lv_color_hex(0xff0000));
    lv_style_set_bg_opa(&style,LV_OPA_100);
    //************************************************
    urp.collection_content = create_midea_collection(THE_DEVICE,urp.root_page,
        LV_DIR_HOR,
        root_pgae_update_collection,
        root_page_install_collection,
        root_page_free_data);
    if(urp.collection_content == NULL){
        debug_d("err0\n");
        return -1;
    }

    midea_collection_view* collection = (midea_collection_view*)urp.collection_content;
    collection->dynamic_load_tag.load_tag = MIDEA_DYNAMIC_LOAD;

    lv_obj_add_style(urp.collection_content, &style, LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(urp.collection_content,LV_OPA_0,LV_PART_MAIN);

    lv_obj_set_style_pad_row(urp.collection_content, 0, 0);
    lv_obj_set_style_pad_column(urp.collection_content, 0, 0);

    lv_obj_add_event_cb(urp.collection_content,root_page_collection_view_callback,LV_EVENT_ALL,NULL);
    //************************************************
    static page_info_t scene;

    scene.method = lv_scene_page_method();
    scene.data   = NULL;
    add_midea_collection_data(THE_DEVICE,&scene,UI_TYPE_SCENE);
    //*********************************
    static page_info_t home;
    home.method  = lv_home_page_method();
    home.data    = NULL;
    add_midea_collection_data(THE_DEVICE,&home,UI_TYPE_HOME);
    //************************************************
    root_page_device_trav();
    //************************************************
    update_midea_collection(THE_DEVICE);

    scroll_collection_view_to_index_anim(THE_DEVICE,1,0);
    //************************************************

    urp.index = 1;
    urp.x_s   = 0;
    urp.x_e   = 0;
    urp.scroll_flag = 0;
    //************************************************

    return 0;
}

static void root_page_on_custom_attr_config(page_base_t* self)
{
    return ;
}
static void root_page_on_view_load(page_base_t* self)
{
    lv_obj_add_flag(self->root,LV_OBJ_FLAG_HIDDEN);

    if(root_page_is_create()){
        root_page_show();
    }else{
        root_page_create();
    }

    return ;
}
static void root_page_on_view_did_load(page_base_t* self)
{
    return ;
}
static void root_page_on_view_will_appear(page_base_t* self)
{
    return ;
}
static void root_page_on_view_did_appear(page_base_t* self)
{
    return ;
}
static void root_page_on_view_will_disappear(page_base_t* self)
{
    return ;
}
static void root_page_on_view_did_disappear(page_base_t* self)
{
    return ;
}
static void root_page_on_view_did_unload(page_base_t* self)
{
    root_page_hide();
    return ;
}

static page_vtable_t root_page_param = {
    .on_custom_attr_config  = root_page_on_custom_attr_config,
    .on_view_load           = root_page_on_view_load,
    .on_view_did_load       = root_page_on_view_did_load,
    .on_view_will_appear    = root_page_on_view_will_appear,
    .on_view_did_appear     = root_page_on_view_did_appear,
    .on_view_will_disappear = root_page_on_view_will_disappear,
    .on_view_did_disappear  = root_page_on_view_did_disappear,
    .on_view_did_unload     = root_page_on_view_did_unload,
};

page_vtable_t* lv_page_root_create(void)
{
    return &root_page_param;
}

int root_page_del_byindex(int index)
{
    remove_midea_collection_data(THE_DEVICE,index);
    return 0;
}

int root_page_update_byindex(int index)
{
    update_midea_collection_with_index(THE_DEVICE,index);
    return 0;
}

int root_page_delete_all_device(void)
{
    int i = 0;
    int dev_num = 0;
    dev_num = midea_collection_get_data_count(THE_DEVICE);
    debug_d("dev_num=%d\n",dev_num);

    dev_num = dev_num - 2;
    for(i=0;i<dev_num;i++)
    {
        remove_midea_collection_data(THE_DEVICE,2);
    }

    return 0;
}

int root_page_add_swled(ui_dev_swled_t* swled)
{
    page_info_t* p_swled = dev_swled_page_reg(swled);
    if(!p_swled){
        debug_e("err\n");
        return -1;
    }

    add_midea_collection_data(THE_DEVICE,p_swled,UI_TYPE_DEV_SW);
    return 0;
}

int root_page_add_dwled(ui_dev_dwled_t* dwled)
{
    page_info_t* p_dwled = dev_dwled_page_reg(dwled);
    if(!p_dwled){
        debug_e("err\n");
        return -1;
    }

    add_midea_collection_data(THE_DEVICE,p_dwled,UI_TYPE_DEV_DW);
    return 0;
}

int root_page_add_devmc(ui_dev_mc_t* mc)
{
    page_info_t* p_mc = dev_mc_page_reg(mc);
    if(!p_mc){
        debug_e("err\n");
        return -1;
    }

    add_midea_collection_data(THE_DEVICE,p_mc,UI_TYPE_DEV_MC);
    return 0;
}

int root_page_add_devoc(ui_dev_oc_t* oc)
{
    page_info_t* p_oc = dev_oc_page_reg(oc);
    if(!p_oc){
        debug_e("err\n");
        return -1;
    }

    add_midea_collection_data(THE_DEVICE,p_oc,UI_TYPE_DEV_OC);
    return 0;
}

int root_page_add_devjc(ui_dev_jc_t* jc)
{
    page_info_t* p_jc = dev_jc_page_reg(jc);
    if(!p_jc){
        debug_e("err\n");
        return -1;
    }

    add_midea_collection_data(THE_DEVICE,p_jc,UI_TYPE_DEV_JC);
    return 0;
}


int root_page_add_devpuri(ui_dev_purifier_t* puri)
{
    page_info_t* p_puri = dev_puri_page_reg(puri);
    if(!p_puri){
        debug_e("err\n");
        return -1;
    }

    add_midea_collection_data(THE_DEVICE,p_puri,UI_TYPE_DEV_PURI);
    return 0;
}

int root_page_add_devwasher(dev_washer_t* washer)
{
    page_info_t* p_washer = dev_washer_page_reg(washer);
    if(!p_washer){
        debug_e("err\n");
        return -1;
    }

    add_midea_collection_data(THE_DEVICE,p_washer,UI_TYPE_DEV_WASHER);
    return 0;
}

int root_page_update_test(void)
{
    root_link_t* dev_link = NULL;
    dev_link = root_page_find_bydevid("js11");
    if(!dev_link){
        debug_d("not find device,id:%s\n","js11");
        return -1;
    }

    ui_puri_data_t* dev_data = NULL;
    dev_data =(ui_puri_data_t*)dev_link->e_attr.dev->priv_data;

    dev_data->status = DEV_WORK;//DEV_OFF_LINE; DEV_OFF DEV_WORK
    dev_data->workstatus = 0;

    dev_data->jstds = 300;//196;
    dev_data->cstds = 300;//196;

    dev_data->f_info[0].life = 500;
    dev_data->f_info[1].life = 600;  
    dev_data->f_info[2].life = 80;

    return 0;
}
